<template>
  <DataList 
  :cate="cate" 
  :cols="cols" 
  ref="datalist"
  :name="name"
  skipExcelNumber="3"
  :excelHeader="excelHeader"/>
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import { get开设专业,update开设专业,delete开设专业,add开设专业 } from '@/api/开设专业.js'
import linq from "linq";
import {findComponent} from "@/utils/ui";
import { addXnjd, deleteXnjd, updateXnjd, getXnjd,bulkAddXnjd } from "@/api/校内实践基地.js";
export default {
  components: {
    DataList
  },
  data() {
    return {
      cate: "A4-1校内实践基地表",
      name:"4.1 校内实践基地",
      excelHeader:{B3:"实践基地名称(全称)",C3:"面向专业",E3:"被列为实训基地项目",G3:"建筑面积（平方米）",H3:"设备值（万元）",J3:"当年设备来源(万元）",M3:"设备数（台套）",O3:"实训项目",Q3:"学年使用频率（人时）",S3:"工位数(个)",T3:"原材料(耗材)费用（万元）",U3:"设备维护费用（万元）",V3:"专职管理人员（名）",W3:"兼职管理人员（名）",C4:"总数（个）",D4:"主要专业",E4:"支持部门",F4:"批准日期（年）",H4:"设备总值",I4:"其中：当年新增设备值",J4:"自主研制设备值",K4:"社会捐赠设备值",L4:"社会准捐赠设备值",M4:"设备总数",N4:"大型设备数",O4:"总数（个）",P4:"主要项目名称(全称)",Q4:"校内",R4:"社会"},
      cols: [
        { label: "实践基地名称(全称)", prop: "XNSJJDMC" },
        { label: "面向专业", prop: "校内实践基地_面向专业",
        data:[
            { label: "总数(个)", prop: "MXZY_ZS"},
            { label: "主要专业", prop: "MXZY_ZYZY"},
        ] },
        { label: "被列为实训基地项目", prop: "校内实践基地_被列为实训基地项目",
        data:[
            { label: "支持部门", prop: "XZ_SXJD_ZCBM"},
            { label: "批准日期(年)", prop: "SXJD_PZRQ"},
        ] },
        { label: "建筑面积(平方米)", prop: "XNSJJSJZMJ" },
        { label: "设备值(万元)", prop: "校内实践基地_设备值",
        data:[
            { label: "设备总值", prop: "XNSJJDSBZZ"},
            { label: "其中：当年新增设备值", prop: "DNXZSBZ"},
        ] },
        { label: "当年设备来源(万元)", prop: "校内实践基地_当年设备来源",
        data:[
            { label: "自主研制设备值", prop: "ZZYZSBZ"},
            { label: "社会捐赠设备值", prop: "SHJZSBZ"},
            { label: "社会准捐赠设备值", prop: "SHZJZSBZ"},
        ] },
        { label: "设备数(台套)", prop: "校内实践基地_设备数",
        data:[
            { label: "设备总数", prop: "SBZS"},
            { label: "大型设备数", prop: "DXSBS"},
        ] },
        { label: "实训项目", prop: "校内实践基地_实训项目",
        data:[
            { label: "总数(个)", prop: "SXXMS"},
            { label: "主要项目名称(全称)", prop: "ZYSXXM"},
        ] },
        { label: "学年使用频率(人时)", prop: "校内实践基地_学年使用频率",
        data:[
            { label: "校内", prop: "XNXNSYPL"},
            { label: "社会", prop: "XNSHSYPL"},
        ] },
        { label: "工位数", prop: "GWS" },
        { label: "原材料(耗材)费用(万元)", prop: "YCLHCF" },
        { label: "设备维护费用(万元)", prop: "WHF" },
        { label: "专职管理人员", prop: "ZZGLRR" },
        { label: "兼职管理人员", prop: "JZGLRR" },
      ]
    };
  },
   
  methods: {
    getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
        sums[0]="合计";
         sums[6]= "基地数:"+linq.from(list).count("$.XNSJJDMC");
          sums[11]= linq.from(list).sum("$.XNSJJSJZMJ").toFixed(2);
         sums[12]= "设备总值:"+linq.from(list).sum("$.XNSJJDSBZZ").toFixed(2);
          sums[13]= linq.from(list).sum("$.DNXZSBZ").toFixed(2);
          sums[14]= linq.from(list).sum("$.ZZYZSBZ").toFixed(2);
          sums[15]= linq.from(list).sum("$.SHJZSBZ").toFixed(2);
          sums[16]= linq.from(list).sum("$.SHZJZSBZ").toFixed(2);
          sums[17]= linq.from(list).sum("$.SBZS");
          sums[18]= linq.from(list).sum("$.DXSBS");
          sums[21]= linq.from(list).sum("$.XNXNSYPL");
          sums[22]= linq.from(list).sum("$.XNSHSYPL");
          sums[23]= linq.from(list).sum("$.GWS");
          sums[24]= linq.from(list).sum("$.YCLHCF").toFixed(2);
          sums[25]= linq.from(list).sum("$.WHF").toFixed(2);
          sums[26]= linq.from(list).sum("$.ZZGLRR");
          sums[27]= linq.from(list).sum("$.JZGLRR");
          sums[28]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
            sums[28] += ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
            sums[28] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
       return sums;
      //
    },
    async onChange(model,field,value){
      console.log(model,field,value,1111)
      if(field=='MXZY_ZYZY'){
        model.MXZY_ZS = value.length+''
      }
    },

    async check(array,list){
      return new Promise(async (resolve,reject)=>{
        
        var 专业 = await get开设专业(this.$root.user.XXID,this.$root.user.CJPC);
        //
        
        array.forEach((e,i)=>{
          array[i].MXZY_ZYZY = array[i].MXZY_ZYZY.replace(/（/g, "(").replace(/）/g, ")")
        });
        
        //验证专业组合
        var err =  linq.from(array).selectMany("$.MXZY_ZYZY.split('、')").except(linq.from(专业).select("$.ZYMC+'('+$.ZYDM+')'").toArray()).toArray();
        linq.from(array).where(e=>linq.from(e.MXZY_ZYZY.split('、')).intersect(err).toArray().length>1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中不存在对应的专业组合,或者中文输入法下的括号");
        //
      
          var err =  linq.from(array).select("$.XNSJJDMC").intersect(linq.from(list).select("$.XNSJJDMC").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.XNSJJDMC) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中已经存在该实验基地名称");
          
         resolve();
      });
    },
    async onDlgCreated(){
     
      // var 专业 = await get开设专业(this.$root.user.XXID,this.$root.user.CJPC);
       var dd= findComponent(this, "MXZY_ZYZY");
      var data = linq.from(this.$root.开设专业).select("{...$,value:$.ZYMC+'（'+$.ZYDM+'）'}").distinct("$.value").toArray();
      dd.$attrs.items = data;
    },
   
    async fetchData() {
      var user = getUser();
      var datas = await getXnjd(user.XXID,user.CJPC);
      datas.forEach(e=>{
        e.MXZY_ZYZY=e.MXZY_ZYZY.split("、");
      })
      return datas;
    },
     async onFetchData(list) {
      list.forEach(e=>{
        e.MXZY_ZYZY=e.MXZY_ZYZY.split("、");
      })
    },
     add_check(model,list){
      //
      
          //是否已经存在数据
          var first =  linq.from(list).firstOrDefault(e=>e.XNSJJDMC ==  model.XNSJJDMC);          
          if(first)
          return "已经存在该实践基地名称"
      //
    },
    onadd(model){
       
     
      var temp = JSON.parse(JSON.stringify(model))
      temp.MXZY_ZYZY = temp.MXZY_ZYZY.join("、")
      return new Promise(async (resolve,reject)=>{
        try{
       
          console.log(JSON.stringify(temp));
          var model = await addXnjd(temp);
           model.MXZY_ZYZY = model.MXZY_ZYZY.split("、")
          resolve(model);
          }catch(e){
        reject(e);
      }
      })
    },
   
    
   async  bulkAdd(array){
       await  bulkAddXnjd({Xnjd:array});
    },
    onupdate(model) {
      //
      
      var temp = JSON.parse(JSON.stringify(model))
      temp.MXZY_ZYZY = temp.MXZY_ZYZY.join("、")
      return new Promise(async (resolve,reject)=>{
          console.log(JSON.stringify(temp));
           await updateXnjd(temp);
         
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
        
         await deleteXnjd(temp);
          resolve();
      })
    }
  }
};
</script>
<style>
</style>